package top.llcngu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @description 服务消费者
 * @author llcngu
 * @date 2021/4/2 23:29
 * @since v1.0
 * @version v1.0
 */
@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @description 消费nacos-standalone-provider服务提供的/provider功能
     * @author llcngu
     * @date 2021/4/3 16:58
     * @return /provider返回的结果
     */
    @GetMapping("/consumer")
    public String test() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-standalone-provider");
        return restTemplate.getForObject(serviceInstance.getUri() + "/provider?name=llcngu", String.class);
    }
}
